package com.dfs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfs.entity.Fruit;
import com.dfs.mapper.FruitMapper;
import com.dfs.service.FruitService;
import com.dfs.util.ColorUtil;
import com.dfs.vo.BarValuesVo;
import com.dfs.vo.BarVo;
import com.dfs.vo.PageQueryVo;
import com.dfs.vo.PieVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dfs
 * @since 2021-09-10
 */
@Service
public class FruitServiceImpl extends ServiceImpl<FruitMapper, Fruit> implements FruitService {

    @Autowired
    private FruitMapper fruitMapper;

    @Override
    public BarVo findFruitBar() {
        List<String> names = new ArrayList<>();// 水果名称
        List<BarValuesVo> values = new ArrayList<>();// sale+itemStyle的值
        // 1.查出数据
        List<Fruit> fruits = fruitMapper.selectList(null);
        // 2.组装数据
        for (Fruit fruit : fruits) {
            names.add(fruit.getName());
            BarValuesVo barValuesVo = new BarValuesVo();
            barValuesVo.setValue(fruit.getSale());
            barValuesVo.setItemStyle(ColorUtil.colorBySaleMap(fruit.getSale()));
            values.add(barValuesVo);
        }
        // 3.将数据填充
        BarVo barVo = new BarVo();
        barVo.setNames(names);
        barVo.setValues(values);
        return barVo;
    }

    @Override
    public IPage<Fruit> page(Integer page, Integer size, PageQueryVo pageQueryVo) {
        Page<Fruit> pageFruit = new Page<Fruit>(page,size);
        QueryWrapper<Fruit> queryWrapper = new QueryWrapper<>();
        if(pageQueryVo != null){
            String name = pageQueryVo.getName();
            if(!StringUtils.isEmpty(name)){
                queryWrapper.like("name",name);
            }
        }
        IPage<Fruit> iPage = fruitMapper.selectPage(pageFruit,queryWrapper);
        List<Fruit> records = iPage.getRecords();
        for (Fruit record : records) {
            System.out.println(record.getName());
            System.out.println(record.getSale());
        }
        return iPage;
    }

    @Override
    public List<PieVo> findFruitPie() {
        List<PieVo> pieVoList = new ArrayList<>();
        List<Fruit> fruits = fruitMapper.selectList(null);
        for (Fruit fruit : fruits) {
            PieVo pieVo = new PieVo();
            pieVo.setName(fruit.getName());
            pieVo.setValue(fruit.getSale());
            pieVoList.add(pieVo);
        }
        return pieVoList;
    }

    @Override
    public IPage<Fruit> findFruitsPage(Page<Fruit> page,String fruitsName) {
        return fruitMapper.findFruitsPage(page,fruitsName);
    }
}
